package com.baymax.test.scene

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ScrollView
import android.widget.TextView
import androidx.core.view.setPadding
import com.airbnb.mvrx.MavericksState
import com.airbnb.mvrx.MavericksViewModel
import com.airbnb.mvrx.MavericksViewModelFactory
import com.baymax.lib.logger.KLog
import com.baymax.test.scene.demo.maverick.argsOrNull
import com.baymax.test.scene.demo.maverick.sceneViewModel
import kotlinx.coroutines.launch

class PlayerTestScene : BaseScene() {
    private val url: String? by argsOrNull<String?>()
    private val vvm: VideoVM by sceneViewModel()

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        val arg = arguments ?: Bundle()
        val context = p1.context
        val scene = this
        println("player arg $arg , url $url")
        p1.clipChildren = true
        p1.clipToPadding = true
        return p1.newSceneFlexContainer {
            setPadding(30)
            clipToPadding = true
            clipChildren = true
            val ed = EditText(context)
            val uu =
                url.orEmpty()
                    .ifBlank { "http://ad.vod01.icntvcdn.com/media/new/2013/icntv2/media/2019/05/14/H142108PhilipsOHCWilliamOutputProdu15sTM.ts" }
            ed.setText(uu)

            val playerBox = Player(context)
            val logger = TextView(context)
            val stringBuilder = StringBuilder()

            playerBox.setLogger {
                playerBox.post {
                    stringBuilder.append(it)
                    stringBuilder.append("\n")
                    logger.text = stringBuilder
                }
                Log.d("Player-V", it)
            }
            addButton("play") {
                vvm.url(ed.editableText.toString())
                playerBox.start(ed.text.toString())
            }
            addButton("stop") {
                playerBox.stop()
            }

            addButton("clear log") {
                logger.text = ""
                stringBuilder.clear()
            }
            val surface = addButton("switch texture") {
                vvm.toggle()
                launch {
                    val s = vvm.awaitState()
                    if (s.surface) {
                        playerBox.switchSurface()
                    } else {
                        playerBox.switchTexture()
                    }
                }
            }
            val decoderBox = addButton("decoder") {
                vvm.toggle()
                launch {
                    val s = vvm.awaitState()
                    if (s.decoderId == Player.Decoder.IJK) {
                        playerBox.switchDecoder(Player.Decoder.Sys)
                    } else {
                        playerBox.switchDecoder(Player.Decoder.IJK)
                    }
                    vvm.toggleDecoder(playerBox.decoderId)
                }
            }

            addView(ed, 600, 100)
            val container = addDFView(v = playerBox) {

            }
            addView(ScrollView(context).apply {
                addView(logger)
            }, -1, 300)

            vvm.onEach(VideoInfo::w, VideoInfo::h) { w, h ->
                KLog.d(TAG, "w:%s,h:%s", w, h)
                container.layoutParams.height = h
                container.layoutParams.width = w
                container.postInvalidate()
            }

            vvm.onEach(VideoInfo::surface) { s ->
                surface.text = "switch ${if (s) "Texture" else "Surface"}"
            }
            vvm.onEach(VideoInfo::decoderId) { s ->
                decoderBox.text = "decoder $s"
            }
        }
    }

    data class VideoInfo(
        val url: String,
        val w: Int = 300,
        val h: Int = 200,
        val surface: Boolean = true,
        val decoderId: Player.Decoder = Player.Decoder.IJK

    ) : MavericksState

    class VideoVM(videoInfo: VideoInfo) : MavericksViewModel<VideoInfo>(videoInfo) {


        fun toggle() {
            setState { copy(surface = !surface) }
        }

        fun url(string: String) {
            setState { copy(url = string) }
        }

        fun toggleDecoder(decoderId: Player.Decoder) {
            setState { copy(decoderId = decoderId) }
        }

        companion object : MavericksViewModelFactory<VideoVM, VideoInfo> {
            override fun create(
                viewModelContext: com.airbnb.mvrx.ViewModelContext,
                state: VideoInfo
            ): VideoVM {
                return VideoVM(state)
            }

            override fun initialState(viewModelContext: com.airbnb.mvrx.ViewModelContext): VideoInfo {
                return VideoInfo("")
            }
        }

    }

}